package com.originskyseed.platform.count.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.originskyseed.platform.common.api.config.LoginConfig;
import com.originskyseed.platform.common.api.constant.RedisKeyConstans;
import com.originskyseed.platform.core.config.bean.ConfigManager;
import com.originskyseed.platform.core.redis.util.RedisUtil;
import com.originskyseed.platform.count.model.FinancialManage;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.count.i18n.CountResponseMsg;
import com.originskyseed.platform.count.service.IFinancialManageService;
import com.originskyseed.platform.count.vo.request.AnchorConversionRateRequestVO;
import com.originskyseed.platform.log.model.HomePageCache;
import com.originskyseed.platform.log.service.IHomePageCacheService;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.RankingListVO;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IAnchorLevelService;
import com.originskyseed.platform.user.service.IAnchorRelateLevelService;
import com.originskyseed.platform.user.service.IAnchorRelateLiveService;
import com.originskyseed.platform.user.service.IAnchorService;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.IParameterConfigureService;
import com.originskyseed.platform.user.service.IPlayerService;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 内容类统计 小组 Controller类
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/contentHomePageGroupCount")
@BaseLocaleResource(localeFileName = "i18n_count")
public class ContentHomePageGroupCountController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ContentHomePageGroupCountController.class);

	@DubboReference
	private IDeptService deptService;

	@DubboReference
	private IUserInfoService userInfoService;

	@DubboReference
	private IPlayerService playerService;

	@DubboReference
	private IAnchorService anchorService;

	@DubboReference
	private IAnchorLevelService anchorLevelService;

	@DubboReference
	private IAnchorRelateLevelService anchorRelateLevelService;

	@DubboReference
	private IAnchorRelateLiveService anchorRelateLiveService;

	@DubboReference
	private IFinancialManageService financialManageService;

	@DubboReference
	private IParameterConfigureService parameterConfigureService;

	@DubboReference
	private IHomePageCacheService homePageCacheService;

	/**
	 * 查询所属公会
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectGroupHomePage.json")
	public BaseResponseBody selectGroupHomePage(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 要返回的对象
			Map<String, Object> map = new HashMap<>();
			String anchorName = null;
			String headPortrait = null;
			Integer anchorNumber = 0;
			Integer addAnchorNumber = 0;
			Integer lastAddAnchorNumber = 0;
			BigDecimal anchorRate = BigDecimal.ZERO;
			BigDecimal anchorAmount = BigDecimal.ZERO;
			BigDecimal lastAnchorAmount = BigDecimal.ZERO;
			BigDecimal amountRate = BigDecimal.ZERO;

			// 当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 部门id
			String deptId = this.getParameterString("deptId", null);
			if (deptId == null) {
				deptId = userInfo.getDeptId();
			}

			if (userInfo != null) {
				anchorName = userInfo.getName();
				headPortrait = userInfo.getHeadPortrait();

				List<String> deptList = new ArrayList<>();
				deptList.add(deptId);
				List<String> anchorIdList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);
				anchorNumber = anchorIdList.size();
				if (anchorIdList.size() > 0) {
					// 查询家族的这个月主播数量和上个月主播数量
					Map<String, Object> anchorQuery = new HashMap<>();
					anchorQuery.put("deptId", deptId);
					anchorQuery.put("startTime", getCurrentMonthStartTime());
					anchorQuery.put("endTime", getCurrentMonthEndTime());
					// 这个月新增主播数量
					addAnchorNumber = this.userInfoService.selectAnchorNumberByTime(anchorQuery);
					// 上月的新增主播数量
					anchorQuery.put("startTime", getLastMonthStartTime());
					anchorQuery.put("endTime", getLastMonthEndTime());
					lastAddAnchorNumber = this.userInfoService.selectAnchorNumberByTime(anchorQuery);
					// 比率
					if(lastAddAnchorNumber!=null&&lastAddAnchorNumber!=0) {
						anchorRate = new BigDecimal((addAnchorNumber - lastAddAnchorNumber) / lastAddAnchorNumber).setScale(2, BigDecimal.ROUND_HALF_UP);
					}
					// 查询家族的流水
					Map<String, Object> query = new HashMap<>();
					query.put("list", anchorIdList);
					query.put("startTime", getCurrentMonthStartTime());
					query.put("endTime", getCurrentMonthEndTime());
					// 本月的营业额
					anchorAmount = this.playerService.selectAnchorProfit(query);
					// 上月的流水
					query.put("startTime", getLastMonthStartTime());
					query.put("endTime", getLastMonthEndTime());
					lastAnchorAmount = this.playerService.selectAnchorProfit(query);
					// 比率
					if(lastAnchorAmount!=null&&lastAnchorAmount.compareTo(BigDecimal.ZERO)>1) {
						amountRate = anchorAmount.subtract(lastAnchorAmount).divide(lastAnchorAmount, 2, BigDecimal.ROUND_HALF_UP);
					}
				}
			}
			map.put("anchorName", anchorName);
			map.put("headPortrait", headPortrait);
			map.put("anchorNumber", anchorNumber);
			map.put("addAnchorNumber", addAnchorNumber);
			map.put("lastAddAnchorNumber", lastAddAnchorNumber);
			map.put("anchorRate", anchorRate);
			map.put("anchorAmount", anchorAmount);
			map.put("lastAnchorAmount", lastAnchorAmount);
			map.put("amountRate", amountRate);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询我的任务
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectMyMission.json")
	public BaseResponseBody selectMyMission(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 要返回的对象
			Map<String, Object> map = new HashMap<>();

			// 当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 部门id
			String deptId = this.getParameterString("deptId", null);
			if (deptId == null) {
				deptId = userInfo.getDeptId();
			}

			if (StringUtils.isNotBlank(deptId)) {
				List<String> deptList = new ArrayList<>();
				deptList.add(deptId);
				List<String> userIdList = this.userInfoService.selectAnchorIdsByDeptIds(deptList);
				if (userIdList.size() > 0) {
					map = this.playerService.selectMyMissionByUserIds(userIdList);
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询KPI
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectKPI.json")
	public BaseResponseBody selectKPI(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {

			// 部门id
			String deptId = this.getParameterString("deptId", null);
			// 类型 0日，1周，2月，3季，4年
			Integer type = this.getParameterInteger("type", null);
			// 要返回的数据
			Map<String, Object> resultMap = null;

			// 从redis中查询是否存在
			if(type==0){
				resultMap = (Map<String, Object>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIDAY");
			} else if (type==1){
				resultMap =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIWEEK");
			} else if(type == 2 ){
				resultMap =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIMONTH");
			}else if(type == 3 ) {
				resultMap =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIQUARTER");
			}else{
				resultMap =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIYEAR");
			}

			// 如果redis里面为空，则查询数据库，并且存进redis里面
			if(resultMap==null){
				resultMap = new HashMap<>();
				List<String> columnList = new ArrayList<>();
				List<BigDecimal> rowList = new ArrayList<>();
				List<BigDecimal> row1List = new ArrayList<>();
				// 获取小组下所有主播
				UserInfo queryUser = new UserInfo();
				queryUser.setType(11);
				queryUser.setDeptId(deptId);
				List<UserInfo> userInfoList = this.userInfoService.selectList(queryUser);
				for (UserInfo userInfo : userInfoList) {
					// 主播id
					List<String> userList = new ArrayList<>();
					userList.add(userInfo.getOtherAccount());

					// 查询主播营收值
					BigDecimal revenueValue;
					if (userList.size() > 0) {
						revenueValue = this.playerService.selectRevenueValue(type, userList, 1);
					} else {
						revenueValue = BigDecimal.ZERO;
					}

					BigDecimal kpi = BigDecimal.ZERO;
					FinancialManage query = new FinancialManage();
					query.setObjId(userInfo.getId());
					query.setType(2);
					query.setStartTime(getMonthFirstDay());
					List<FinancialManage> financialManageList = this.financialManageService.selectList(query);
					for (FinancialManage financialManage : financialManageList) {
						kpi = kpi.add(financialManage.getAmount());
					}
					if (type == 0) {
						kpi = kpi.divide(new BigDecimal(30), 2, BigDecimal.ROUND_HALF_UP);
					} else if (type == 1) {
						kpi = kpi.divide(new BigDecimal(4), 2, BigDecimal.ROUND_HALF_UP);
					} else if (type == 3) {
						kpi = kpi.multiply(new BigDecimal(4));
					} else if (type == 4) {
						kpi = kpi.multiply(new BigDecimal(12));
					}

					rowList.add(revenueValue);
					row1List.add(kpi);
					columnList.add(userInfo.getName());
				}
				resultMap.put("columnList", columnList);
				resultMap.put("kipCompleteList", rowList);
				resultMap.put("kipTargetList", row1List);

				// 存进redis中
				if(type==0){
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIDAY",resultMap,getCacheInvalidTime());
				} else if (type==1){
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIWEEK",resultMap,getCacheInvalidTime());
				} else if(type == 2 ){
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIMONTH",resultMap,getCacheInvalidTime());
				}else if(type == 3 ) {
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIQUARTER",resultMap,getCacheInvalidTime());
				}else{
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectKPIYEAR",resultMap,getCacheInvalidTime());
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询工会财务数据
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectFinancialData.json")
	public BaseResponseBody selectFinancialData(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			Map<String, Object> map = (Map<String, Object>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectFinancialData");

			//redis里面没有则直接查询，并且存进redis里面
			if(map == null){
				map = new HashMap<>();
				List<String> columnList = new ArrayList<>();
				List<BigDecimal> rowList = new ArrayList<>();
				List<BigDecimal> row1List = new ArrayList<>();
				List<BigDecimal> row2List = new ArrayList<>();
				// 获取所有主播的id
				List<String> deptList = new ArrayList<>();
				deptList.add(deptId);
				List<String> anchorIdList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);
				Calendar cal = Calendar.getInstance();
				int year = cal.get(Calendar.YEAR);
				// 查询工会每月的营收、kpi
				Map<String, Object> query = new HashMap<>();
				query.put("list", anchorIdList);
				for (int i = 1; i < 13; i++) {
					String beginTime = getBeginTime(year, i);
					String endTime = getEndTime(year, i);
					// 查询工会营收
					BigDecimal revenueValue = BigDecimal.ZERO;
					if (anchorIdList.size() > 0) {
						query.put("startTime", beginTime);
						query.put("endTime", endTime);
						revenueValue = this.playerService.selectAnchorProfit(query);
					}
					// 查询
					BigDecimal kpi = BigDecimal.ZERO;
					FinancialManage queryKpi = new FinancialManage();
					queryKpi.setObjId(deptId);
					queryKpi.setType(2);
					queryKpi.setStartTime(sdf.parse(beginTime));
					List<FinancialManage> financialManageList = this.financialManageService.selectList(queryKpi);
					for (FinancialManage financialManage : financialManageList) {
						kpi = kpi.add(financialManage.getAmount());
					}
					columnList.add(i + "");
					rowList.add(revenueValue);
					row1List.add(kpi);
					if (kpi.compareTo(BigDecimal.ZERO) != 0) {
						row2List.add(revenueValue.divide(kpi, 2));
					} else {
						row2List.add(new BigDecimal(1));
					}
				}
				map.put("columnList", columnList);
				map.put("kpiCompleteList", rowList);
				map.put("kpiTargetList", row1List);
				map.put("kpiCompletionSchedule", row2List);

				// 存进redis里面
				RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectFinancialData",map,getCacheInvalidTime());
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询当前收益
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectTodayAchievement.json")
	public BaseResponseBody selectTodayAchievement(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			Map<String, Object> map = (Map<String, Object>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectTodayAchievement");

			//redis里面没有则直接查询，并且存进redis里面
			if(map == null){
				map = new HashMap<>();
				if (StringUtils.isNotBlank(deptId)) {
					List<String> deptList = new ArrayList<>();
					deptList.add(deptId);
					List<String> anchorIdList = this.userInfoService.selectAnchorIdsByDeptIds(deptList);
					// 今日日期
					String today = getToday();
					// 昨天日期
					String yesterday = getYesterday();

					BigDecimal todayAmount = BigDecimal.ZERO;
					BigDecimal yesterdayAmount = BigDecimal.ZERO;
					if (anchorIdList.size() > 0) {
						// 今日消费总额，昨日消费总额
						Map<String, Object> query = new HashMap<>();
						query.put("list", anchorIdList);
						query.put("startTime", today);
						query.put("endTime", today);
						todayAmount = this.playerService.selectAnchorProfit(query);
						query.put("startTime", yesterday);
						query.put("endTime", yesterday);
						yesterdayAmount = this.playerService.selectAnchorProfit(query);
					}

					// 计算比率
					map.put("todayProfit", todayAmount.multiply(new BigDecimal("0.3")));
					map.put("yesterdayProfit", yesterdayAmount.multiply(new BigDecimal("0.3")));
					if (yesterdayAmount.compareTo(BigDecimal.ZERO) != 0) {
						map.put("rate", todayAmount.subtract(yesterdayAmount).divide(yesterdayAmount, 2));
					} else {
						map.put("rate", 1);
					}

					// 存进redis里面
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectTodayAchievement",map,getCacheInvalidTime());
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询当前排行
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectCurrentRanking.json")
	public BaseResponseBody selectCurrentRanking(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			Map<String, Object> map = (Map<String, Object>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectCurrentRanking");

			//redis里面没有则直接查询，并且存进redis里面
			if(map == null){
				map = new HashMap<>();
				int rank = 0;
				BigDecimal amount = BigDecimal.ZERO;
				if (StringUtils.isNotBlank(deptId)) {
					// 查询同级所有的小组
					List<String> deptList = this.selectGroupId(deptId);

					List<RankingListVO> list = this.playerService.selectLabourUnionRanking(deptList);
					if (list.size() > 0) {
						rank = list.size() + 1;
						for (int i = 0; i < list.size(); i++) {
							RankingListVO rankingListVO = list.get(i);
							if (deptId.equals(rankingListVO.getDeptId())) {
								rank = rankingListVO.getRank();
								if (i > 0) {
									amount = list.get(i - 1).getAmount().subtract(rankingListVO.getAmount());
								}
							}
						}
					} else {
						rank = 1;
					}
				}
				map.put("rank", rank);
				map.put("amount", amount);

				// 存进redis里面
				RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectCurrentRanking",map,getCacheInvalidTime());
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询房间曝光量
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectRoomExposure.json")
	public BaseResponseBody selectRoomExposure(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);
			// 类型 0日，1周，2月，3季，4年
			Integer type = this.getParameterInteger("type", null);

			// 要返回的数据
			Map<String, Object> map = null;

			// 从redis中查询是否存在
			if(type==0){
				map = (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRoomExposureDAY");
			} else if (type==1){
				map =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRoomExposureWEEK");
			} else{
				map =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRoomExposureMONTH");
			}

			// 如果redis里面为空，则查询数据库，并且存进redis里面
			if(map==null){
				map = new HashMap<>();
				if (StringUtils.isNotBlank(deptId)) {
					// 查询同级小组的所有主播id
					List<String> deptList = this.selectGroupId(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);
					if (anchorList.size() > 0) {
						map = this.playerService.selectVisitsNumber(type, anchorList);
						// 存进redis里面
						if(type==0){
							RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRoomExposureDAY",map,getCacheInvalidTime());
						} else if (type==1){
							RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRoomExposureWEEK",map,getCacheInvalidTime());
						} else{
							RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRoomExposureMONTH",map,getCacheInvalidTime());
						}
					}
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 分页查询主播转换率
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAnchorConversionRateByPage.json")
	public BaseResponseBody selectAnchorConversionRateByPage(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			AnchorConversionRateRequestVO requestVO = this.getParameterBean(AnchorConversionRateRequestVO.class);

			// 当前用户信息
			UserInfo currentUserInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String deptId = this.getParameterString("deptId", null);
			if (deptId == null) {
				deptId = currentUserInfo.getDeptId();
			}

			// 分页查询
			BasePage<Map<String, Object>> page = new BasePage<Map<String, Object>>(requestVO.getPageNo(), requestVO.getPageSize());
			if (StringUtils.isNotBlank(deptId)) {
				List<String> deptList = new ArrayList<>();
				deptList.add(deptId);
				List<String> anchorIdList = this.userInfoService.selectAnchorIdsByDeptIds(deptList);
				if (anchorIdList.size() > 0) {
					requestVO.setList(anchorIdList);
					page = this.playerService.selectAnchorConversionRateByPage(requestVO.toMap(), page);

					// 获取数据，3日留存率，7日留存率
					Map<String, Object> map = new HashMap<>();
					map.put("startTime", getDateByDay(3));
					map.put("endTime", getToday());
					for (Map<String, Object> objectMap : page.getResults()) {
						objectMap.put("oneRate", objectMap.get("changeRate"));
						map.put("anchorId", objectMap.get("anchorId"));
						Integer threeVisitsNum = this.playerService.selectVisitsNumberByTime(map);
						Integer threeConsumeNum = this.playerService.selectConsumeUserNum(map);
						objectMap.put("threeRate", new BigDecimal((float) threeConsumeNum / threeVisitsNum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

						map.put("startTime", getDateByDay(7));
						Integer sevenVisitsNum = this.playerService.selectVisitsNumberByTime(map);
						Integer sevenConsumeNum = this.playerService.selectConsumeUserNum(map);
						objectMap.put("sevenRate", new BigDecimal((float) sevenConsumeNum / sevenVisitsNum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
					}
				}
			}

			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询首次进房排行榜
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectFirstGetintoHousingRanking.json")
	public BaseResponseBody selectFirstGetintoHousingRanking(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			List<RankingListVO> rankingListVOList = (List<RankingListVO>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectFirstGetintoHousingRanking");

			// redis里面没有则直接查询，并且存进redis里面
			if(rankingListVOList==null){
				int size = this.getParameterInteger("size", 10);
				if (StringUtils.isNotBlank(deptId)) {
					Dept dept = this.deptService.selectByPrimaryKey(deptId);
					// 查询小组的所有主播id
					List<String> deptList = new ArrayList<>();
					deptList.add(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);
					if (anchorList.size() > 0) {
						Map<String, Object> query = new HashMap<>();
						query.put("size", size);
						query.put("list", anchorList);
						rankingListVOList = this.playerService.selectFirstGetintoHousingRanking(query);
						for (RankingListVO rankingListVO : rankingListVOList) {
							rankingListVO.setDeptName(dept.getName());
						}

						// 存进redis中
						RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectFirstGetintoHousingRanking",rankingListVOList,getCacheInvalidTime());
					}
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), rankingListVOList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询转换率排行榜
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectConversionRateRanking.json")
	public BaseResponseBody selectConversionRateRanking(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			List<RankingListVO> rankingListVOList = (List<RankingListVO>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectConversionRateRanking");

			// redis里面没有则直接查询，并且存进redis里面
			if(rankingListVOList==null){
				int size = this.getParameterInteger("size", 10);
				if (StringUtils.isNotBlank(deptId)) {
					Dept dept = this.deptService.selectByPrimaryKey(deptId);
					// 查询小组的所有主播id
					List<String> deptList = new ArrayList<>();
					deptList.add(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);

					if (anchorList.size() > 0) {
						Map<String, Object> query = new HashMap<>();
						query.put("size", size);
						query.put("list", anchorList);
						rankingListVOList = this.playerService.selectConversionRateRanking(query);
						for (RankingListVO rankingListVO : rankingListVOList) {
							rankingListVO.setDeptName(dept.getName());
						}

						// 存进redis中
						RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectConversionRateRanking",rankingListVOList,getCacheInvalidTime());
					}
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), rankingListVOList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 主播排麦直播时长排行
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectRowWheatLiveTime.json")
	public BaseResponseBody selectAnchorLiveTime(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);
			// 类型 0日，1周，2月
			Integer type = this.getParameterInteger("type", null);

			// 要返回的数据
			Map<String, Object> map = null;

			// 从redis中查询是否存在
			if(type==0){
				map = (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRowWheatLiveTimeDAY");
			} else if (type==1){
				map =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRowWheatLiveTimeWEEK");
			} else{
				map =  (Map<String, Object>)RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRowWheatLiveTimeMONTH");
			}

			// 如果redis里面为空，则查询数据库，并且存进redis里面
			if(map==null){
				map = new HashMap<>();
				List<String> columnList = new ArrayList<>();
				List<BigDecimal> rowList = new ArrayList<>();
				if (StringUtils.isNotBlank(deptId)) {
					// 查询小组的所有主播id
					List<String> deptList = new ArrayList<>();
					deptList.add(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);
					if (anchorList.size() > 0) {
						List<Map<String, Object>> list = this.playerService.selectAnchorLiveTime(type, anchorList, 2);
						for (Map<String, Object> objectMap : list) {
							columnList.add((String) objectMap.get("name"));
							BigDecimal minute = (BigDecimal) objectMap.get("time");
							rowList.add(minute.divide(new BigDecimal(60), 2, BigDecimal.ROUND_HALF_UP));
						}
					}
				}
				map.put("columnList", columnList);
				map.put("rowList", columnList);

				// 存进redis里面
				if(type==0){
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRowWheatLiveTimeDAY",map,getCacheInvalidTime());
				} else if (type==1){
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRowWheatLiveTimeWEEK",map,getCacheInvalidTime());
				} else{
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectRowWheatLiveTimeMONTH",map,getCacheInvalidTime());
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 主播个人直播时长排行
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectPersonalLiveTime.json")
	public BaseResponseBody selectPersonalLiveTime(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);
			// 类型 0日，1周，2月
			Integer type = this.getParameterInteger("type", null);

			// 从redis中查询是否存在
			Map<String, Object> map = (Map<String, Object>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectPersonalLiveTime");

			//redis里面没有则直接查询，并且存进redis里面
			if(map == null){
				map = new HashMap<>();
				List<String> columnList = new ArrayList<>();
				List<Float> rowList = new ArrayList<>();
				if (StringUtils.isNotBlank(deptId)) {
					// 查询小组的所有主播id
					List<String> deptList = new ArrayList<>();
					deptList.add(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);

					if (anchorList.size() > 0) {
						List<Map<String, Object>> list = this.playerService.selectAnchorLiveTime(type, anchorList, 1);
						for (Map<String, Object> objectMap : list) {
							columnList.add((String) objectMap.get("name"));
							int minute = (int) objectMap.get("time");
							rowList.add((float) minute / 60);
						}
					}
				}
				map.put("columnList", columnList);
				map.put("rowList", columnList);

				// 存进redis里面
				RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectPersonalLiveTime",map,getCacheInvalidTime());
			}

			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询优质主播排行
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectQualityAnchorRanking.json")
	public BaseResponseBody selectQualityAnchorRanking(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			List<RankingListVO> rankingListVOList = (List<RankingListVO>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectQualityAnchorRanking");

			// redis里面没有则直接查询，并且存进redis里面
			if(rankingListVOList==null){
				int size = this.getParameterInteger("size", 50);
				if (StringUtils.isNotBlank(deptId)) {
					// 查询同级小组的所有主播id
					List<String> deptList = this.selectGroupId(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);

					if (anchorList.size() > 0) {
						Map<String, Object> query = new HashMap<>();
						query.put("size", size);
						query.put("list", anchorList);
						rankingListVOList = this.playerService.selectQualityAnchorRanking(query);
					}

					// 存进redis里面
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectQualityAnchorRanking",rankingListVOList,getCacheInvalidTime());
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), rankingListVOList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 公会用户消费TOP榜
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectConsumptionRanking.json")
	public BaseResponseBody selectConsumptionRanking(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 部门id
			String deptId = this.getParameterString("deptId", null);

			// 从redis中查询是否存在
			List<RankingListVO> rankingListVOList = (List<RankingListVO>) RedisUtil.getInstance().getForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectConsumptionRanking");
			// redis里面没有则直接查询，并且存进redis里面
			if(rankingListVOList==null){
				int size = this.getParameterInteger("size", 50);
				if (StringUtils.isNotBlank(deptId)) {
					// 查询同级小组的所有主播id
					List<String> deptList = this.selectGroupId(deptId);
					List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);

					if (anchorList.size() > 0) {
						Map<String, Object> query = new HashMap<>();
						query.put("size", size);
						query.put("list", anchorList);
						if (anchorList.size() > 0) {
							rankingListVOList = this.playerService.selectConsumptionRanking(query);
						}
					}

					// 存进redis里面
					RedisUtil.getInstance().setForObject(RedisKeyConstans.HOME_PAGE_KEY+deptId+"selectConsumptionRanking",rankingListVOList,getCacheInvalidTime());
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), rankingListVOList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询自带粉排行
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAnchorByoFans.json")
	public BaseResponseBody selectAnchorByoFans(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 要返回的集合
			List<Map<String, Object>> list = new ArrayList<>();

			// 获取主播名称
			String name = this.getParameterString("name", null);
			// 当前用户信息
			UserInfo currentUserInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String deptId = this.getParameterString("deptId", null);

			if (deptId == null) {
				deptId = currentUserInfo.getDeptId();
			}
			if (StringUtils.isNotBlank(deptId)) {
				// 查询小组的所有主播id
				List<String> deptList = new ArrayList<>();
				deptList.add(deptId);
				List<String> anchorList = this.userInfoService.selectAnchorOtherIdsByDeptIds(deptList);

				if (anchorList.size() > 0) {
					Map<String, Object> query = new HashMap<>();
					query.put("name", name);
					query.put("list", anchorList);
					list = this.playerService.selectAnchorByoFans(query);
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_SUCCESS.getMsg()), list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SELECT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询渠道下所有小组
	 * 
	 * @param id
	 * @throws Exception
	 */
	private List<String> selectGroupId(String id) throws Exception {
		List<String> list = new ArrayList<>();
		list.add(id);
		Dept dept = this.deptService.selectByPrimaryKey(id);
		if (dept != null) {
			List<Dept> deptList = this.deptService.selectByParentCode(dept.getParentCode());
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
			}
		}
		return list;
	}

	// 日期格式化
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 获得本周的第一天，周一
	 *
	 * @return
	 */
	public String getToday() {
		Calendar c = Calendar.getInstance();
		return sdf.format(c.getTime());
	}

	/**
	 * 获得昨天的日期
	 *
	 * @return
	 */
	public String getYesterday() {
		Calendar c = Calendar.getInstance();
		try {
			c.add(Calendar.DATE, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sdf.format(c.getTime());
	}

	/**
	 * 获得几天前的日期
	 *
	 * @return
	 */
	public String getDateByDay(int day) {
		Calendar c = Calendar.getInstance();
		try {
			c.add(Calendar.DATE, -day);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sdf.format(c.getTime());
	}

	/**
	 * 获得本月的开始时间
	 *
	 * @return
	 */
	public String getCurrentMonthStartTime() {
		Calendar c = Calendar.getInstance();
		try {
			c.set(Calendar.DATE, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sdf.format(c.getTime());
	}

	/**
	 * 本月的结束时间
	 *
	 * @return
	 */
	public String getCurrentMonthEndTime() {
		Calendar c = Calendar.getInstance();
		try {
			c.set(Calendar.DATE, 1);
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.DATE, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sdf.format(c.getTime());
	}

	/**
	 * 获得上月的开始时间
	 *
	 * @return
	 */
	public String getLastMonthStartTime() {
		Calendar c = Calendar.getInstance();
		try {
			c.add(Calendar.MONTH, -1);
			c.set(Calendar.DATE, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sdf.format(c.getTime());
	}

	/**
	 * 上月的结束时间
	 *
	 * @return
	 */
	public String getLastMonthEndTime() {
		Calendar c = Calendar.getInstance();
		try {
			c.set(Calendar.DATE, 1);
			c.add(Calendar.DATE, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sdf.format(c.getTime());
	}

	/**
	 * 获得月份开始时间
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public String getBeginTime(int year, int month) {
		YearMonth yearMonth = YearMonth.of(year, month);
		LocalDate localDate = yearMonth.atDay(1);
		LocalDateTime startOfDay = localDate.atStartOfDay();
		startOfDay.format(DateTimeFormatter.BASIC_ISO_DATE);
		ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));

		return sdf.format(Date.from(zonedDateTime.toInstant()));
	}

	/**
	 * 获得月份结束时间
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public String getEndTime(int year, int month) {
		YearMonth yearMonth = YearMonth.of(year, month);
		LocalDate endOfMonth = yearMonth.atEndOfMonth();
		LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
		ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
		return sdf.format(Date.from(zonedDateTime.toInstant()));
	}

	/**
	 * 查询本月第一天
	 * 
	 * @return
	 * @throws Exception
	 */
	private Date getMonthFirstDay() throws Exception {
		Calendar c = Calendar.getInstance();
		try {
			c.set(Calendar.DATE, 1);
			c.set(Calendar.HOUR_OF_DAY, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			c.set(Calendar.MILLISECOND, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}


	/**
	 * 获取失效时间
	 * @return
	 */
	private long getCacheInvalidTime(){
		LoginConfig loginConfig = ConfigManager.getInstance().parseObject(LoginConfig.class);
		long timeOut = 7200000;
		if(loginConfig!=null && loginConfig.getCacheInvalidTime()!=null){
			timeOut = loginConfig.getCacheInvalidTime()*60*1000;
		}
		return timeOut;
	}
}